package com.bingxue.edu.framework.exception;

import cn.hutool.core.util.StrUtil;
import com.bingxue.edu.framework.common.constant.HttpStatus;
import lombok.Getter;

/**
 * 错误码异常，一般不直接使用，而是使用其子类
 *
 * @author panbo
 * @since 2024/6/26
 */
public abstract class ErrorCodeException extends RuntimeException {

    /**
     * 错误码
     */
    private final ErrorCode errorCode;

    public abstract int getStatusCode();

    public ErrorCodeException(ErrorCode errorCode, Object... params) {
        super(StrUtil.format(errorCode.getMessage(), params));
        this.errorCode = errorCode;
    }

    public ErrorCodeException(ErrorCode errorCode, String message) {
        super(message);
        this.errorCode = errorCode;
    }

    public ErrorCodeException(ErrorCode errorCode, Throwable cause) {
        super(StrUtil.format(errorCode.getMessage(), cause.getMessage()), cause);
        this.errorCode = errorCode;
    }

    public ErrorCodeException(ErrorCode errorCode, String message, Throwable cause) {
        super(message, cause);
        this.errorCode = errorCode;
    }

    public int getErrorCode() {
        return errorCode.getCode();
    }

    public boolean isSystemError() {
        return errorCode.isSystemError();
    }

    /**
     * 错误消息，用于提示用户，系统级错误隐藏错误详情
     */
    public String getErrorMessage() {
        return this.getMessage();
    }
}
